// 验证请求的数据

module.exports = {
    // 处理必填的数据
    checkNull: (data, msg, len, sex) => {
        if(typeof data == "number")return { msg: "验证成功", data, code: "200" }
        // data = data.replace(/(^\s*)|(\s*$)/g, "");
        if (!data) return { msg: "请输入" + msg, code: "400" }
        if (!data.length > 0) return { msg: "请输入" + msg, code: "400" }
        if (sex) {
            if (data != "男") {
                if (data != "女") {
                    return { msg: "验证性别失败，只能是男或者女", code: "400" }
                }
            }
        }
        if (len) {
            if (data.length > len) return { msg: msg + "长度不能超过" + len + "位", code: "400" }
            return { msg: "验证成功", data, code: "200" }
        } else {
            return { msg: "验证成功", data, code: "200" }
        }
    },
    // 检测是否是 JSON 格式
    isJson: function (obj, msg) {
        if (!obj) return { msg: "请输入" + msg, code: "400" }
        if (typeof obj == "object") {
            try {
                JSON.stringify(obj)
                return { msg: "验证成功", data: obj, code: "200" }
            } catch (e) {
                return { msg: "验证失败" + msg + "不是JSON格式", code: "400" }
            }
        } else if (typeof obj == "string") {
            try {
                JSON.parse(obj)
                return { msg: "验证成功", data: JSON.stringify(obj), code: "200" }
            } catch (e) {
                return { msg: msg + "不是JSON格式", code: "400" }
            }
        }
    },
    // 处理 pageSize 和 pageNum
    handleNumber: (data, msg) => {
        if (!data) return { msg: msg + " is undefind", code: "400" }
        // data = data.replace(/\s*/g, "");
        if (!data) return { msg: msg + " 不能是空格", code: "400" }
        data = Number(data)
        if (isNaN(data)) return { msg: msg + " is NaN", code: "400" }
        return { msg: "验证成功", data, code: "200" }
    },

    // 验证手机号
    checkPhone: (phone, msg) => {
        let reg_pgone = /^(?:(?:\+|00)86)?1\d{10}$/;
        if (reg_pgone.test(phone)) return { msg: "验证" + msg + "成功", data: phone, code: "200" }
        return { msg: "请输入正确的" + msg, code: "400" }
    },
    // 验证邮箱
    checkMail: (mail, msg) => {
        let reg_mail = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
        if (reg_mail.test(mail)) return { msg: "验证" + msg + "成功", data: mail, code: "200" }
        return { msg: "请输入正确的" + msg, code: "400" }
    },
    // 验证时间戳
    checkTimeStamp: (data, msg) => {
        data = data.replace(/(^\s*)|(\s*$)/g, "");
        if (data) {
            if (data.length === 13) {
                data = Number(data)
                return { msg: "验证成功", data, code: "200" }
            } else if (data.length === 10) {
                data = Number(data)
                return { msg: "验证成功", data, code: "200" }
            } else {
                return { msg: "请输入" + msg, code: "400" }
            }
        } else {
            return { msg: "请输入" + msg, code: "400" }
        }
    }
}